package com.HQCourse.vod.vod.controller;

import com.HQCourse.Exception.HQCourseException;
import com.HQCourse.result.Result;
import com.HQCourse.vod.vod.service.VodService;
import com.HQCourse.vod.vod.utils.ConstantPropertiesUtils;
import com.HQCourse.vod.vod.utils.Signature;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Random;

@Api(tags = "腾讯云点播")
@RestController
@RequestMapping("/admin/vod")
//@CrossOrigin
public class VodController {
    @Autowired
    private VodService vodService;
    //上传视频接口

    @PostMapping("upload")
    public Result uploadVideo(
            @ApiParam(name = "file", value = "文件", required = true)
            @RequestParam("file") MultipartFile file) throws IOException {
        InputStream inputStream = file.getInputStream();
        String originalFilename = file.getOriginalFilename();
        String videoId = vodService.uploadVideo(inputStream, originalFilename);
        return Result.ok(videoId);
    }
    @DeleteMapping("delete/{id}")
    private Result<String> deleteVod(@PathVariable String id){
        String filed=vodService.deleteId(id);
        return Result.ok(filed);
    }
    @PostMapping("update")
    public Result uploadVod(){
        String filed=vodService.VodUpload();
        return Result.ok(filed);
    }
    @ApiOperation("还回签名")
    @GetMapping("sign")
    public Result sign(){
        Signature sign = new Signature();
        // 设置 App 的云 API 密钥
        sign.setSecretId(ConstantPropertiesUtils.ACCESS_KEY_ID);
        sign.setSecretKey(ConstantPropertiesUtils.ACCESS_KEY_SERET);
        sign.setCurrentTime(System.currentTimeMillis() / 1000);
        sign.setRandom(new Random().nextInt(java.lang.Integer.MAX_VALUE));
        sign.setSignValidDuration(3600 * 24 * 2); // 签名有效期：2天


        try {
            String signature = sign.getUploadSignature();
            System.out.println("signature : " + signature);
            return Result.ok(signature);
        } catch (Exception e) {
            System.out.print("获取签名失败");
            e.printStackTrace();
            throw new HQCourseException(20001, "获取签名失败");
        }
    }
}
